import * as types from '../constants/LoginTypes'
import {loginGo} from "../../api/api";
import {storage} from '../../utils/storage'

function loginSuccess(token) {
    return {
        type:types.LOGINSUCCESS,
        token:token
    }
}
function loginFail() {
    return {
        type:types.LOGINFAIL
    }
}
function logining() {
    return {
        type:types.LOGINING
    }
}

function login(data) {
    return dispatch => {
        dispatch(logining())
        return loginGo(data).then((response) => {
                console.log('[dispatch]',response)
                storage.save('token',response.token)
                dispatch(loginSuccess(response.token))
            }).catch(err => {
                console.log('[err]',err)
                dispatch(loginFail())
            })
    }
}

export default {
    loginSuccess,
    loginFail,
    logining,
    login
}